// ALLOCAZIONE FISICA DELLE RISORSE MEGA103
/* TABELLA RIASSUNTIVA EFFETTIVA ALLOCAZIONE
PIN    NAME    ALIAS    FUNCTION    IO    DESCRIPTION    
      
    P1.0 -       I
    P1.1 -       I
    P1.2 -       I
    P1.3 -       I
    P1.4 -       I
    P1.5 -       I
    P1.6 -       I
    P1.7 -       O
    
    P2.0 -       I
    P2.1 -       I
    P2.2 -       I
    P2.3 -       I
    P2.4 -       I
    P2.5 -       I
    P2.6 -       I
    P2.7 -       O

    P3.0 -       I
    P3.1 -       I
    P3.2 -       I
    P3.3 -       I
    P3.4 -       I
    P3.5 -       I
    P3.6 -       I
    P3.7 -       O

    P4.0 -       I
    P4.1 -       I
    P4.2 -       I
    P4.3 -       I
    P4.4 -       I
    P4.5 -       I
    P4.6 -       I
    P4.7 -       O

    P5.0 -       I
    P5.1 -       I
    P5.2 -       I
    P5.3 -       I
    P5.4 -       I
    P5.5 -       I
    P5.6 -       I
    P5.7 -       O
 
    P6.0 SR_Clk  O
    P6.1 SR_RClk O
    P6.2 SR_Out  O
    P6.3 LED_DL3 O
    P6.4 LED_DL2 O
    P6.5 LED_DL1 O
    P6.6 P2      I
    P6.7 P1      I
    
*/

//----------------------------------------------
// TASTIERA
#define    KEY_PORT        PINA
#define    KEY_PORT_DDR    DDRA
 
//----------------------------------------------
// SHIFT REGISTER
//
#define    SR_PORT        PORTC

#define    SR_Clk         0x00
#define    LEDLine_RClk   0x01
#define    SR_Out_FireDrv 0x02
#define    FireDrv_RClk   0x03
#define    SR_Out_LEDDrv  0x04
#define    LEDDrv_RClk    0x05

#define    SR_Clk         0x06
#define    SR_Clear       0x07

#define    Buffer_OE_Port PORTD
#define    Buffer_OE      0x07
 
//----------------------------------------------
// ADC 
//
#define    ADC_PORT        PORTF
#define    ADC_PORT_PIN    PINF
#define    VBat_Sense        0x00
#define    HiVoltage_Sense   0x01
#define    Test5_Sense        0x02
#define    Test4_Sense        0x04
#define    Test3_Sense        0x08
#define    Test2_Sense        0x10
#define    Test1_Sense        0x20

//----------------------------------------------
// INVERTER 
//
#define    INV_PORT        PORTD
#define    CTL_INVERTER        0x06

 
//----------------------------------------------
// FIRE MODE 
//

#define    FIRE_PORT      PORTD
#define    FireMode_Sense        0x03
#define    SetFireMode    0x05


#define    SetPortBit(fPORTA,fBIT)        ((fPORTA)|= (1<<(fBIT)))
#define    ClrPortBit(fPORTA,fBIT)        ((fPORTA)&= (~(1<<(fBIT))))
#define    ToglePortBit(fPORTA,fBIT)      ((fPORTA)^= (1<<(fBIT)))
#define    GetPortBit(fPORTA,fBIT)        (((fPORTA) & (1<<(fBIT)))==0)

// Definizione dei Bitfield IO

struct sAnalogIn  { 
        char    VBatSense:1;
        char    HiVoltageSense:1;
        char    Test5Sense:1;
        char    Test4Sense:1;
        char    Test3Sense:1;
        char    Test2Sense:1;
        char    Test1Sense:1;
};


struct sPort02  { 
        char    bit0:1;
        char    bit1:1;
        char    bit2:1;
        char    FireModeSense:1;
        char    bit4:1;
        char    FireMode:1;
        char    Disable:1;                
        char    bit7:1;
};


 
#define    FirePort    ( * ( struct sPort02 *) 0x32)
#define    AnalogPort  ( * ( struct sAnalogIn *) 0x20)
#define    FireInput   ( * ( struct sPort02 *) 0x30)
#define    InvPort     ( * ( struct sPort02 *) 0x32)


#define    VBatt2Num(Vb)      ((unsigned int)((Vb * 1024)/ 18.5) )
#define    HiV2Num(Vh)        ((unsigned int)((Vh * 1024.0)/ 440) )


